package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicDeviceAttributeDTO;
import com.iwomy.secureplat.platform.entity.BasicDeviceAttributeEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicDeviceAttributeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备属性
 *
 * @author pig
 * @date 2023-11-14 10:34:52
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicDeviceAttribute" )
@Tag(description = "basicDeviceAttribute" , name = "设备属性管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicDeviceAttributeController {

    private final  BasicDeviceAttributeService basicDeviceAttributeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param deviceAttributeDTO 设备属性
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttribute_view')" )
    public R getBasicDeviceAttributePage(@ParameterObject Page page, @ParameterObject BasicDeviceAttributeDTO deviceAttributeDTO) {
        return R.ok(basicDeviceAttributeService.getBasicDeviceAttributePage(page, deviceAttributeDTO));
    }

    /**
     * 通过id查询设备属性
     * @param  id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttribute_view')" )
	public R getById(@PathVariable("id" ) Long id) {
		return R.ok(basicDeviceAttributeService.getById(id));
	}

    /**
     * 新增设备属性
     * @param basicDeviceAttribute 设备属性
     * @return R
     */
    @Operation(summary = "新增设备属性" , description = "新增设备属性" )
    @SysLog("新增设备属性" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttribute_add')" )
    public R save(@RequestBody BasicDeviceAttributeEntity basicDeviceAttribute) {
		BasicDeviceAttributeEntity wrapperEntity = new BasicDeviceAttributeEntity();
		wrapperEntity.setAttributeName(basicDeviceAttribute.getAttributeName());
		wrapperEntity.setDeviceTypeId(basicDeviceAttribute.getDeviceTypeId());
		LambdaQueryWrapper<BasicDeviceAttributeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicDeviceAttributeEntity one = basicDeviceAttributeService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)){
//			throw new PlatformException("设备属性已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.ATTRIBUTE_ID_EXIST));
		}
		basicDeviceAttribute.setDeptId(SecurityUtils.getUser().getDeptId());
        return R.ok(basicDeviceAttributeService.save(basicDeviceAttribute));
    }

    /**
     * 修改设备属性
     * @param basicDeviceAttribute 设备属性
     * @return R
     */
    @Operation(summary = "修改设备属性" , description = "修改设备属性" )
    @SysLog("修改设备属性" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttribute_edit')" )
    public R updateById(@RequestBody BasicDeviceAttributeEntity basicDeviceAttribute) {
        return R.ok(basicDeviceAttributeService.updateById(basicDeviceAttribute));
    }

    /**
     * 通过id删除设备属性
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除设备属性" , description = "通过id删除设备属性" )
    @SysLog("通过id删除设备属性" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttribute_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(basicDeviceAttributeService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicDeviceAttribute 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicDeviceAttribute_export')" )
    public List<BasicDeviceAttributeEntity> export(BasicDeviceAttributeEntity basicDeviceAttribute,Long[] ids) {
        return basicDeviceAttributeService.list(Wrappers.lambdaQuery(basicDeviceAttribute).in(ArrayUtil.isNotEmpty(ids), BasicDeviceAttributeEntity::getId, ids));
    }
}